package com.fas.system.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.fas.system.domain.FasCooperationTrade;
import com.fas.system.mapper.FasCooperationTradeMapper;
import com.fas.system.service.IFasCooperationTradeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FasCooperationTradeServiceImpl implements IFasCooperationTradeService {

    @Resource
    private FasCooperationTradeMapper fasCooperationTradeMapper;

    @Override
    public List<Tree<String>> dicTrade(){
        List<FasCooperationTrade> list = fasCooperationTradeMapper.findAll();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("sort");
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setChildrenKey("children");
        //转换器
        return TreeUtil.build(list, "-1", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getCode());
                    tree.setParentId(treeNode.getParentCode());
                    tree.setWeight(treeNode.getOrderNo());
                    tree.setName(treeNode.getValue());
                });
    }
}
